package com.tnsw.noxnox.settopbox;

import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

import org.apache.http.impl.DefaultConnectionReuseStrategy;
import org.apache.http.impl.DefaultHttpResponseFactory;
import org.apache.http.impl.DefaultHttpServerConnection;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.BasicHttpProcessor;
import org.apache.http.protocol.HttpRequestHandlerRegistry;
import org.apache.http.protocol.HttpService;

import com.tnsw.coreutils.AsyncManager;
import com.tnsw.coreutils.http.THttpBase;

import android.content.Context;
import android.util.Log;

public class STBWebserver extends Thread {

	public static final int USE_BUILTIN_HANDLER = 1;
	public static final int USE_CUSTOM_HANDLER = 2;
	
	private String tag = "STBWebserver";
	protected Context context;
	protected AsyncManager asyncManager;
	
    protected int port;
	private BasicHttpProcessor httpProc;
	private BasicHttpContext httpContext;
	private HttpService httpService;
	private HttpRequestHandlerRegistry registry = null;
	protected boolean isRunning = false;
	private int mode = USE_BUILTIN_HANDLER;
	
	private ArrayList<HttpRequestHandlerRegistry> arrHandlers;
	
	public STBWebserver(Context context, int port) {
		this.context = context;
		this.port = port;
		this.tag = this.tag + ":" + String.valueOf(port);
		
		this.asyncManager = AsyncManager.getInstance();
		
		this.arrHandlers = new ArrayList<HttpRequestHandlerRegistry>();
		this.httpProc = new BasicHttpProcessor();
		this.httpContext = new BasicHttpContext();
		
		this.httpService = new HttpService(this.httpProc, new DefaultConnectionReuseStrategy(), 
				new DefaultHttpResponseFactory());
						
		this.registry = new HttpRequestHandlerRegistry();
		//this.registry.register("/q/*", new STBWebHandler(context));
		
		this.httpService.setHandlerResolver(this.registry);
		Log.d(tag, "Started listener");
		

	}
	
	public void setMode(int mode) {
		this.mode = mode;
	}
	
	public int getMode() {
		return(this.mode);
	}
	
	public HttpRequestHandlerRegistry getHttpRegistry() {
		return(this.registry);
	}
	
	public synchronized void startThread() {
		this.isRunning = true;
		super.start();
	}
	
	protected void processCustomHandler(Context context, Socket socket) {
		
	}
	
	@Override
	public void run() {
		super.run();
		
		try {
			ServerSocket serverSocket = new ServerSocket(this.port);
			Log.d(tag, "listening at port: " + this.port);
			serverSocket.setReuseAddress(true);
			
			while (this.isRunning) {
				try {
					Socket socket = serverSocket.accept();
					//socket.setKeepAlive(true);
					//socket.setSoTimeout(30000);
					Log.d(tag, "Accepted new socket");
					
					if (this.mode == USE_BUILTIN_HANDLER) {
						Log.d(tag, "Using BUILTIN_HANDLER");
						DefaultHttpServerConnection serverConnection = new DefaultHttpServerConnection();
						serverConnection.bind(socket, new BasicHttpParams());
						
						BasicHttpContext hContext = new BasicHttpContext();
						
						this.httpService.handleRequest(serverConnection, hContext);
						
						//serverConnection.shutdown();	
					} else {
						Log.d(tag, "Socket=" + socket.hashCode());
						this.processCustomHandler(this.context, socket);
						
						//socket.close();
					}
					
					
				} catch (Exception e) {
					Log.e(tag, "run() - inside loop - " + e.toString(), e);
				}
			}
		} catch (Exception e) {
			Log.e(tag, "run() - outside loop - " + e.toString(), e);
		}
	}
}
